package com.aistar.order.controller;

import com.aistar.common.util.IdWorker;
import com.aistar.common.vo.Result;
import com.aistar.order.entity.OrderInfo;
import com.aistar.order.service.IOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2019/6/26 0026.
 */
@RestController
public class OrderInfoController {

    @Autowired
    private IOrderInfoService service;
    @Autowired
    private IdWorker idWorker;

//    找到登录用户的所有常用取票人 返回json
    @GetMapping("/orderinfo/{userId}")
    @ResponseBody
    public List<OrderInfo> list (@PathVariable("userId")String userId){
        List<OrderInfo> list = service.findAll(userId);
        return list;
    }
////    更具id找到一个常用取票人
//    @GetMapping("/orderinfo/{inofId}")
//    @ResponseBody
//    public OrderInfo find(@PathVariable("inofId")String infoId) {
//        OrderInfo orderInfo = service.findById(infoId);
//        return orderInfo;
//    }
//    保存取票人信息 不填加重复的
    @PostMapping("/orderinfo")
    @ResponseBody
    public Result save(OrderInfo orderInfo){
        orderInfo.setId(idWorker.nextId());
        orderInfo.setUserId("1");
       boolean flag =   service.save(orderInfo);
        if(flag){
            Result result= new Result(true,2000,"添加联系人成功",orderInfo);
            return result;
        }else{
            Result result= new Result(true,5000,"已存在联系人",orderInfo);
            return result;
        }

    }

//删除取票人
    @DeleteMapping("/orderinfo/{id}")
    @ResponseBody
    public OrderInfo del(@PathVariable("id")String id) {
    service.delete(id);
    return null;
    }
//    修改取票人信息
    @PutMapping("/orderinfo")
    public OrderInfo update(OrderInfo orderInfo){

        service.update(orderInfo);
        return orderInfo;
}
}



